/*
 * @Author: szx
 * @Date: 2021-11-22 13:35:12
 * @LastEditTime: 2021-11-22 13:47:54
 * @Description:
 * @FilePath: \leetcode\1-99\22\22.js
 */
/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
    const res = [];
    const dfs = function (left, right, str) {
        if (left == n && right == n) {
            res.push(str);
        } else {
            if (left > right) {
                if (left < n) {
                    dfs(left + 1, right, str + '(');
                }
                dfs(left, right + 1, str + ')');
            } else if (left == right) {
                dfs(left + 1, right, str + '(');
            }
        }
    };
    dfs(0, 0, '');
    return res;
};
console.log(generateParenthesis(2));
